/*******************************************************************************
 * Copyright (C) 2010 Devin Samarin.
 * 
 * This file is part of the SUIT Toolkit.
 * 
 * The SUIT Toolkit is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * The SUIT Toolkit is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with the SUIT Toolkit.  If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/
import org.json.me.JSONException;
import org.json.me.JSONObject;

import suit.ui.Application;
import suit.ui.Box;
import suit.ui.Button;
import suit.ui.Entry;
import suit.ui.Label;
import suit.ui.Screen;
import suit.ui.ScrolledWindow;
import suit.ui.themes.DefaultTheme;

public class Dictionary extends Application implements Button.Clicked {

	Screen main;

	Box vbox;
	Box hbox;

	Entry entry;
	Button button;
	ScrolledWindow scroller;
	Label definition;

	public Dictionary() {

		main = new Screen(new DefaultTheme());

		hbox = new Box(Box.Horiz, true, 10);
		vbox = new Box(Box.Vert, true, 10);

		entry = new Entry();
		button = new Button("Load");
		button.connect(this);

		hbox.pack(entry, true, true, 0);
		hbox.pack(button, false, true, 0);

		scroller = new ScrolledWindow(true, false);
		scroller.setBorderWidth(10);

		definition = new Label(
				"Enter the word into the box above and press 'Load' to get a dictionary definition.");
		definition.setAlign(Label.TOP | Label.LEFT);

		vbox.pack(hbox, false, true, 0);
		vbox.pack(definition, true, true, 0);

		scroller.add(vbox);

		main.add(scroller);
		setScreen(main);

	}

	public boolean onKeyPressed(int key) {
		if (key == -8)
			notifyDestroyed();
		return false;
	}

	public void onClicked(Button source) {
		String word = entry.getText();
		try {
			definition.setText(new JSONObject().put("word", word).toString());
		} catch (JSONException e) {
			definition.setText(e.getMessage());
		}
	}

}
